<?php
/**
 * Created by PhpStorm.
 * User: zhangtao
 * Date: 16-5-24
 * Time: 下午7:36
 */

namespace Home\Controller;


use Think\Controller;

class AddressController extends Controller
{
    public function index()
    {
        $address = M('address');
        $addressData = $address->field('username',true)->where(['username'=>I('post.username')])->select();
        $this->ajaxReturn($addressData);
    }
    public function add()
    {
        //$data = I('post.');
        $address = M('address');
        if ($address->create()) {
            $result=$address->add();
            if($result) {
                $this->ajaxReturn(array('status' => 'success','id'=>$result));
            }
            else
                $this->ajaxReturn(array('status' => 'fail'));
        } else {
            $this->ajaxReturn(array('status' => 'fail'));
        }
    }
    public function update()
    {
        //$data = I('post.');
        $address = M('address');
        if ($address->create()) {
            if ($address->save())
                $this->ajaxReturn(array('status' => 'success'));
            else
                $this->ajaxReturn(array('status' => 'fail'));
        } else {
            $this->ajaxReturn(array('status' => 'fail'));
        }
    }

    public function delete()
    {
        $data = I('post.');
        $address = M('address');
        if ($address->where($data)->delete())
            $this->ajaxReturn(array('status' => 'success'));
        else
            $this->ajaxReturn(array('status' => 'fail'));
    }
}